package com.jzx.provider.ucenter.consul.service;

import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author daihuihui
 * @date 2019/4/9
 */
@Service
@Slf4j
public class ConsulOssService {

    private OSSClient ossClient = null;

    private final String ENDPOINT = "http://oss-cn-beijing.aliyuncs.com/";

    private final String KEYID = "LTAI4Fg1otwPw5eYwwHqR7CR";

    private final String SECRET = "nNm4egN5yScv52bipBpv62Xc6XDr4v";

    private final String BUCKETNAME = "consul";

    @PostConstruct
    public void init() {
        ossClient = new OSSClient(ENDPOINT,
                KEYID, SECRET);
    }

    /**
     * 下载文件
     */
    public InputStream downLoad(String key) {
        if (!ossClient.doesObjectExist(BUCKETNAME, key)) {
            return null;
        }
        return ossClient.getObject(BUCKETNAME, key).getObjectContent();
    }

    /**
     * 上传研报
     */
    public void upLoad(InputStream inputStream, String key) {
        ossClient.putObject(BUCKETNAME, key, inputStream);
    }


    /**
     * @return void
     * @Description 上传文件到OSS
     * @author zhangting
     * @params [oriUrl, fileUrl]
     * @date 2019/6/11 9:46 AM
     */
    public void upLoadFile(String oriUrl, String fileUrl) {

        try {
            //判断oss是否存在该文件
            if (!ossClient.doesObjectExist(BUCKETNAME, fileUrl)) {
                URL url = new URL(oriUrl);
                URLConnection conn = url.openConnection();
                InputStream inStream = conn.getInputStream();
                this.upLoad(inStream, fileUrl);
            }
        } catch (Exception e) {

        }
    }

    public boolean doesObjectExist(String key) {
        return ossClient.doesObjectExist(BUCKETNAME, key);
    }

    /**
     * 将漏掉的照片重新上传OSS服务器
     *
     * @param ori_url 爬取地址
     */
    public void reUpLoad(String ori_url, String key) {
        try {
            URL url = new URL(ori_url);
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            this.upLoad(inStream, key);
        } catch (Exception e) {

        }
    }




    /**
     * 将InputStream写入本地文件
     *
     * @param destination 写入本地目录
     * @param input       输入流
     * @throws IOException
     */
    public void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        input.close();
    }

    /**
     * multiFile 转成file
     *
     * @param multiFile
     * @return
     */
    public File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        if (StringUtils.isEmpty(fileName)) {
            return null;
        }
        InputStream ins = null;

        String filePath = "/Users/admin/Desktop/test/" + fileName;
        String newFilePath = "/tmp" + fileName;
        File newFile = new File(filePath);
        File AnotherFile = new File(filePath);

        OutputStream os = null;
        try {
            multiFile.transferTo(newFile);
            os = new FileOutputStream(AnotherFile);
            FileInputStream is = new FileInputStream(newFile);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            try {
                while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return AnotherFile;
    }




    /**
     * 文件输入流转字节
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public byte[] read(InputStream inputStream) throws IOException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int num = inputStream.read(buffer);
            while (num != -1) {
                baos.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            baos.flush();
            return baos.toByteArray();
        } finally {
        }
    }
}



